<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2024/3/14
 * Time: 16:31
 */
namespace app\modules\bg\controllers;
use app\common\enum\SetWorkspacesSjqq_ztEnum;
use app\common\service\Running;
use app\common\service\TiktokDataGet;
use app\modules\setting\models\workspaces\SetWorkspacesSjqq;
use yii\base\Exception;
use yii\console\Controller;

class IndexController extends Controller
{
    /**
     * @var Running
     */
    public $running_obj;
    public function beforeAction($action)
    {
        $this->running_obj = new Running(\Yii::$app->controller->action->id);
        if($this->running_obj->isRunning()){
//            echo "任务已执行";
            return false;
        }
        $this->running_obj->runStart();
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    public function afterAction($action, $result)
    {
        $this->running_obj->runEnd();
        return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
    }
    // cd /www/wwwroot/data_analysis_service/api &&  php yii /bg/index/step1
    //后台请求api,第一步
    public function actionStep1(){

        $obj = new TiktokDataGet();
        $query = SetWorkspacesSjqq::find()->andWhere(['zt'=>SetWorkspacesSjqq_ztEnum::UNDO])->limit(100);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetWorkspacesSjqq
                 */
                try{
                    $obj->curl($row);
                }catch (\Throwable $e){
                    $row->zt = SetWorkspacesSjqq_ztEnum::RESPONSE_ERROR;
                    $row->err_msg = $e->getMessage();
                    $row->save();
                }
            }
        }
    }

    public function actionStep2(){
        $obj = new TiktokDataGet();
        $query = SetWorkspacesSjqq::find()->andWhere(['zt'=>SetWorkspacesSjqq_ztEnum::RESPONSE_UNDO])->limit(100);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetWorkspacesSjqq
                 */
                try{
                    $obj->curlRes($row);
                }catch (\Throwable $e){
                    $row->zt = SetWorkspacesSjqq_ztEnum::RESPONSE_ERROR2;
                    $row->err_msg = $e->getMessage();
                    $row->save();
                }
            }
        }
    }

    public function actionStep3(){
        $obj = new TiktokDataGet();
        $query = SetWorkspacesSjqq::find()->andWhere(['zt'=>SetWorkspacesSjqq_ztEnum::RESPONSE_WAIT_ANALYSIS])->limit(100);
        foreach ($query->batch(10) as $batch){
            foreach ($batch as $row){
                /**
                 * @var $row SetWorkspacesSjqq
                 */
                try{
                    $obj->analysis($row);
                }catch (\Throwable $e){
                    $row->zt = SetWorkspacesSjqq_ztEnum::RESPONSE_ANALYSIS_ERROR;
                    $row->err_msg = $e->getMessage();
                    $row->save();
                }
            }
        }
    }
}